package flow.action;

import flow.model.FlowDefine;
import flow.service.FlowDefineService;
import m.common.action.ActionMeta;
import m.common.action.VueResult;
import m.common.model.util.ModelCheckUtil;
import m.common.model.util.ModelQueryList;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.element.attr.ButtonType;
import m.html.element.other.Param;
import m.html.form.*;
import m.html.list.*;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.action.ManageAction;

@ActionMeta(name="flow/flowDefine")
public class FlowDefineAction extends ManageAction {
	private String oper;
	private FlowDefine model;

	public JSONMessage getDefine() throws Exception {
		return getService(FlowDefineService.class).getDefine(model);
	}
	public JSONMessage doSave() throws Exception {
		systemLog("流程","保存流程定义信息");
		verifyAdminOperPower("manage_flow_power");
		String msg=getService(FlowDefineService.class).save(model);
		return new JSONMessage(0,msg)
			.push("model", model);
	}
	public JSONMessage doIssue() throws Exception {
		systemLog("流程","发布流程定义信息");
		verifyAdminOperPower("manage_flow_power");
		getService(FlowDefineService.class).issue(model);
		return new JSONMessage(0,"发布成功")
			.push("model.oid", model.getOid())
			.push("model.issueStatus", model.getIssueStatus());
	}
	public JSONMessage doDelete() throws Exception {
		systemLog("流程","删除流程定义信息");
		verifyAdminOperPower("manage_flow_power");
		getService(FlowDefineService.class).delete(model);
		return new JSONMessage(0,"删除成功");
	}

	public VueResult toEdit() throws Exception {
		verifyMenu("A3101");
		verifyAdminOperPower("manage_flow_power");
		if(null!=model&&!StringUtil.isSpace(model.getOid())){
			model=ModelQueryList.getModel(model,"*","startSection.*");
		}else if(null!=model&&!StringUtil.isSpace(model.getIdentity())){
			ModelCheckUtil.checkUniqueCombine(model, new String[]{"identity"},"标识已存在");
			model.setIssueStatus("C");
			model.setVersion(1);
		}else {
			throw new MException(this.getClass(), "请输入标识");
		}

		ModelForm form = new ModelForm("流程定义",3);
		form.addItem(
			FormHtml.ins("标识","{{model.identity}} V{{model.version}}"),
			FormInput.input("名称", "model.name").setPlaceholder("请输入名称").setRequired(true),
			FormTest.test("oper=='edit'",
					FormInput.selectOneModel("开始环节","model","startSection",
					"'"+actionUrl(FlowSectionAction.class,"selectSection")+"?params[flowDefineOid]='+model.oid",600,
					"{{model.startSection.identity}} {{model.startSection.name}}"))
				.other(FormHtml.ins("开始环节","{{model.startSection.identity}} {{model.startSection.name}}")),
			FormInput.textarea("描述", "model.description").setPlaceholder("请输入描述").setSpan(3)
		)
		.addData("model",model)
		.addData("oper",oper)
		.addButton(
			FormButton.pri("保存","doSave").setTest("oper=='edit'"),
			FormButton.succ("发布","doIssue").setTest("oper=='edit'")
		)
		.addMethod(
			FormEvent.backTrue,
			FormEvent.verify("verifyName",FormJsTest.falseRemind("this.model.name","名称不能为空")),
			FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
				.fillData("model").before("verifyName").success("loadSectionPage"),
			FormEvent.doJson("doIssue",actionUrl("doIssue"),"{model:this.model}")
				.success("backTrue")
		);
		LoadPage sectionPage=new LoadPage("loadSectionPage",
			actionUrl(FlowSectionAction.class,"flowSectionList"),"{params:{flowDefineOid:this.model.oid,oper:this.oper}}")
			.setMounted("this.loadSectionPage();");
		VueFormResult result=new VueFormResult();
		return result.addForm(form).addForm(sectionPage);
	}
	public VueResult toNewEdit() throws Exception {
		systemLog("流程","生成新流程定义信息");
		verifyMenu("A3101");
		model=getService(FlowDefineService.class).saveNew(model);
		return toEdit();
	}
	public VueResult createFlowDefine() throws Exception {
		verifyAdminOperPower("manage_flow_power");

		ModelForm form = new ModelForm("创建流程",1);
		form.addItem(
			FormInput.input("标识", "identity").setPlaceholder("请输入标识").setRequired(true)
		)
		.addData("identity","")
		.addButton(FormButton.pri("下一步","toNext"))
		.addMethod(
			FormEvent.backFlag,
			FormEvent.verify("verifyIdentity",
				FormJsTest.falseRemind("this.identity","标识不能为空"),
				FormJsTest.falseRemind("/^[\\da-z\\-_]+$/i.test(this.identity)","标识只能输入数字字母和横线")
			),
			FormEvent.toModal("toNext",actionUrl("toEdit"),"{model:{identity:this.identity},oper:'edit'}",1200)
					.after("backFlag").before("verifyIdentity")
		);
		VueFormResult result=new VueFormResult();
		return result.addForm(form);

	}
	public VueResult flowDefineList() throws Exception{
		verifyMenu("A3101");
		boolean power=hasAdminOperPower("manage_flow_power");
		VueListResult result=getVueListResult("流程定义列表",FlowDefine.class,actionUrl("flowDefineList"));
		result.setSubTitle("添加或者编辑流程信息。");
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("标识", "identity",true));
		result.addCol(toLinkColumn("名称", "name",true,
			new ListEvent(actionUrl("toEdit"), EventType.MODAL,1200).addParam("model.oid","oid")
		));
		result.addCol(toTextColumn("描述", "description",200,true));
		result.addCol(toTextColumn("发布状态", "issueStatus",true).setDictOption(FlowDefine.class,"issueStatus"));
		result.addCol(toDateColumn("发布时间", "issueDate",true).setSort(true));
		result.addCol(toIntegerColumn("版本", "version").setCount(ListCount.NONE).setSort(true));
		if(power) result.addCol(toSpaceColumn("操作",150,
			ListElement.test("record.issueStatus=='C'", 
				ListButton.modal("修改", ButtonType.PRIMARY,actionUrl("toEdit"),1200, SuccessMethod.REFRESH)
					.addParam("model.oid","oid").addQueryParam(Param.value("oper", "edit"))
			),
			ListElement.elseTest("record.issueStatus=='Y'",
					ListButton.modal("生成新版本",ButtonType.PRIMARY,actionUrl("toNewEdit"),1200,SuccessMethod.MUST_REFRESH)
					.addParam("model.oid","oid").addQueryParam(Param.value("oper", "edit")).setConfirm("确定要生成该流程的新版本吗?")
			),
			ListElement.test("record.issueStatus=='C'", 
				ListButton.ajax("删除",ButtonType.ERROR,actionUrl("doDelete"),SuccessMethod.REFRESH)
					.addParam("model.oid","oid").setConfirm("确定要删除该流程吗?")
			)
		));
		if(power) result.addButton(
			ListButton.modal("新增",ButtonType.PRIMARY,actionUrl("createFlowDefine"),500,SuccessMethod.REFRESH)
		);
		//result.addCondition(QueryCondition.in("issueStatus", "C","Y"));
		return result;
	}
	public FlowDefine getModel() {
		return model;
	}

	public String getOper() {
		return oper;
	}
	public void setOper(String oper) {
		this.oper = oper;
	}
	public void setModel(FlowDefine model) {
		this.model = model;
	}
	
}
